package com.serviceprovider.business.module.order.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.order.services.interfaces.IOrderSettingsService;
import com.serviceprovider.business.module.order.valueobjects.OrderAttributeVO;
import com.serviceprovider.business.utility.RequestConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

//~--- JDK imports ------------------------------------------------------------

import java.beans.PropertyEditorSupport;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;

/**
 * @author mrduongnv@gmail.com
 */
@Controller
@RequestMapping(value = RequestConstants.OrderRequests.VIEW_ORDERS_SETTINGS)
public class ViewOrderSettingsController {
    @Autowired
    private IOrderSettingsService orderSettingsService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public String getAsText() {
                Date             value      = (Date) getValue();
                SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

                dateFormat.setLenient(false);

                if (value != null) {
                    return dateFormat.format(value);
                } else {
                    return null;
                }
            }
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

                dateFormat.setLenient(false);

                try {
                    setValue(dateFormat.parse(text));
                } catch (ParseException e) {
                    setValue(null);
                }
            }
        });
    }

    /**
     * Modified by NehaSiroya
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView viewOrderSettings() {
        System.out.println("viewOrderSettings()");

        return refreshList();
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView addNewOrderFilter() {
        ModelAndView           result       = new ModelAndView(RequestConstants.OrderRequests.VIEW_ORDERS_SETTINGS_NEW_VIEW);
        OrderSettingsFieldForm newFieldForm = new OrderSettingsFieldForm();

        // TODO: Assumed default order types, needs to be validated
        // newFieldForm.type = getOrderTypes();
        // result.addObject("type", type);
        newFieldForm.type = orderSettingsService.findAll();
        result.addObject("newFieldForm", newFieldForm);
        System.out.println("addNewOrderFilter() method called");

        return result;
    }

    @RequestMapping(
        method = RequestMethod.POST,
        params = "add"
    )
    public ModelAndView saveNewOrderFilter(@ModelAttribute OrderSettingsFieldForm form) {
        if (form.getId() == 0) {
            orderSettingsService.addNew(form.convertToVO());
        }

        return refreshList();
    }

    @RequestMapping(
        method = RequestMethod.POST,
        params = "cancel_add"
    )
    public ModelAndView cancelNewOrderFilter(@ModelAttribute OrderSettingsFieldForm form) {
        return refreshList();
    }

    /**
     * Added by NehaSiroya
     *
     * @return
     */
    private ModelAndView refreshList() {
        ModelAndView           result = new ModelAndView(RequestConstants.OrderRequests.VIEW_ORDERS_SETTINGS_VIEW);
        List<OrderAttributeVO> types  = orderSettingsService.findAll();

        result.addObject("types", types);
        System.out.println("/admin/order/view_orders_settings_new = refreshList()");

        return result;
    }
}



